package dao.impl;

import dao.PhoneDao;
import entity.Phone;
import util.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

public class PhoneDaoImpl implements PhoneDao {

    @Override
    public List<Phone> selectAllPhone() throws SQLException {
        return JdbcUtil.executeQuery(Phone.class,"select * from phone");
    }

    @Override
    public void insertPhone(Phone phone) throws SQLException {
        JdbcUtil.executeUpdate("insert into phone(inventory,name,model,price,time) values(?,?,?,?,?)",
                phone.getInventory(),phone.getName(),phone.getModel(),phone.getPrice(),phone.getTime());
    }

    @Override
    public void updatePhoneById(Phone phone) throws SQLException {
        JdbcUtil.executeUpdate("update phone set inventory = ?,name = ?,model=?,price=?,time=? where id=?",
                phone.getInventory(),phone.getName(),phone.getModel(),phone.getPrice(),phone.getTime(),phone.getId());
    }

    @Override
    public void delectPhoneById(int id) throws SQLException {
        JdbcUtil.executeUpdate("delete from phone where id = ?",id);
    }

    @Override
    public Phone selectPhoneById(int id) throws SQLException {
        return JdbcUtil.queryOne(Phone.class,"select * from phone where id = ?",id);
    }
}
